package com.example.sample.logic;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.sample.R;

import java.util.ArrayList;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 
 * ViewPager适配器类 
 * @author  LiangYu
 * @data:  2015-1-5 下午6:43:49 
 * @version:  V1.0
 */
public class MyPagerAdapter extends PagerAdapter {

    public final static String TAG = "MyPagerAdapter";
	private Context mContext; 
	
	private ArrayList<View> mViewList = new ArrayList<View>();
	
	public MyPagerAdapter(Context ctx){
		mContext = ctx;
		
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li;
		li = (LayoutInflater)mContext.getSystemService(infService);
		View v1 = li.inflate(R.layout.activity_viewpager1, null);
		View v2 = li.inflate(R.layout.activity_viewpager2, null);
        TextView v3 = new TextView(mContext);
        v3.setText("第3个");
        TextView v4 = new TextView(mContext);
        v4.setText("第4个");
        TextView v5 = new TextView(mContext);
        v5.setText("第5个");
		mViewList.add(v1);
		mViewList.add(v2);
        mViewList.add(v3);
        mViewList.add(v4);
        mViewList.add(v5);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mViewList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub
		// return super.instantiateItem(container, position);

        Log.i(TAG, "instantiateItem " + position);

		View v = mViewList.get(position);
		((ViewPager)container).addView(v);
		return v;
	}
	
	@Override
	public void destroyItem(View container, int position, Object object) {
		// super.destroyItem(container, position, object);

        Log.i(TAG, "destroyItem " + position);

		View v = mViewList.get(position);
		((ViewPager)container).removeView(v);
	}

}
